#!/bin/bash
# emerge documents accroding to corresponding sub-topics.
# Usage: ./merge_topic.sh  lan_dir

if [ $# -ne 2 ];
then
    echo "Usaga: ./merge_topic.sh language_dir, output_dir"
    exit 1
elif [ ! -d $1 ];
then
    echo "Error: $1 doesn't exist, please check!"
    exit 1
elif [ ! -d $2 ];
then
    echo "Warning: $2 seems doesn't exist, create it"
    mkdir -p $2
fi

loop=1
suffix=`ls $1 | head -n 1 | cut -d '.' -f 2`

for file in `ls $1`
do
    sub_topic="${file:0:4}.$suffix"   # extract the first 4 characters like: M001
    file_path="`realpath $1`/$file"
    sub_topic_path="`realpath $2`/$sub_topic"
#    echo "cat $file_path to $sub_topic_path"
    cat $file_path >> $sub_topic_path
    loop=$(($loop+1))
done
echo "merge done!"

